package com.c2b.algorithm.leetcode.base;

/**
 * <a href="https://leetcode.cn/problems/merge-two-sorted-lists/">合并两个有序链表(Merge Two Sorted Lists)</a>
 * <p>将两个升序链表合并为一个新的 <b>升序</b> 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 </p>
 * <p>
 * <b>示例：</b>
 * <pre>
 * 示例 1：
 *      输入：l1 = [1,2,4], l2 = [1,3,4]
 *      输出：[1,1,2,3,4,4]
 *
 * 示例 2：
 *      输入：l1 = [], l2 = []
 *      输出：[]
 *
 * 示例 3：
 *      输入：l1 = [], l2 = [0]
 *      输出：[0]
 * </pre>
 * </p>
 * <p>
 * <b>提示：</b>
 * <ul>
 *  <li>两个链表的节点数目范围是 [0, 50]</li>
 *  <li>-100 <= Node.val <= 100</li>
 *  <li>l1 和 l2 均按 非递减顺序 排列</li>
 * </ul>
 * </p>
 *
 * @author c2b
 * @see LC0002AddTwoNumbers_M 两数相加(Add Two Numbers)
 * @see LC0019RemoveNthNodeFromEndOfList_M 删除链表的倒数第 N 个结点(Remove Nth Node From End of List)
 * @see LC0021MergeTwoSortedLists_S 合并两个有序链表(Merge Two Sorted Lists)
 * @see LC0023MergeKSortedLists 合并 K 个升序链表(Merge k Sorted Lists)
 * @see LC0024SwapNodesInPairs_M 两两交换链表中的节点(Swap Nodes in Pairs)
 * @see LC0025ReverseNodesInKGroup_H K 个一组翻转链表(Reverse Nodes in k-Group)
 * @see LC0138CopyListWithRandomPointer_M 随机链表的复制(Copy List with Random Pointer)
 * @see LC0141LinkedListCycle_S 环形链表(Linked List Cycle)
 * @see LC0142LinkedListCycle_II_M 环形链表 II(Linked List Cycle II)
 * @see LC0146LRUCache_M LRU 缓存(LRU Cache)
 * @see LC0148SortList_M 排序链表(Sort List)
 * @see LC0160IntersectionOfTwoLinkedLists_S 相交链表(Intersection of Two Linked Lists)
 * @see LC0206ReverseLinkedList_S 反转链表(Reverse Linked List)
 * @see LC0234PalindromeLinkedList_S 回文链表(Palindrome Linked List)
 * @since 2023/5/5 10:36
 */
public class LC0021MergeTwoSortedLists_S {
    static class Solution {
        public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
            //return mergeTwoListsByIterator(list1, list2);
            return mergeTwoListsByRecursion(list1, list2);
        }

        private ListNode mergeTwoListsByIterator(ListNode list1, ListNode list2) {
            ListNode dummyHead = new ListNode(-1);
            ListNode currNode = dummyHead;
            while (list1 != null && list2 != null) {
                if (list1.val <= list2.val) {
                    currNode.next = list1;
                    list1 = list1.next;
                } else {
                    currNode.next = list2;
                    list2 = list2.next;
                }
                currNode = currNode.next;
            }
            currNode.next = list1 == null ? list2 : list1;
            return dummyHead.next;
        }

        private ListNode mergeTwoListsByRecursion(ListNode list1, ListNode list2) {
            if (list1 == null) {
                return list2;
            }
            if (list2 == null) {
                return list1;
            }
            if (list1.val <= list2.val) {
                list1.next = mergeTwoListsByRecursion(list1.next, list2);
                return list1;
            } else {
                list2.next = mergeTwoListsByRecursion(list1, list2.next);
                return list2;
            }
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        ListNode list1 = new ListNode(1);
        list1.next = new ListNode(2);
        list1.next.next = new ListNode(4);
        ListNode list2 = new ListNode(1);
        list2.next = new ListNode(3);
        list2.next.next = new ListNode(4);

        Printer.printListNode(solution.mergeTwoLists(list1, list2));
    }
}
